package com.ruoyi.controller.equipment;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.equipment.MesEquipmentMaintenance;
import com.ruoyi.service.equipment.IMesEquipmentMaintenanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备维护记录Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/equipment/maintenance")
public class MesEquipmentMaintenanceController extends BaseController
{
    @Autowired
    private IMesEquipmentMaintenanceService mesEquipmentMaintenanceService;

    /**
     * 查询设备维护记录列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:maintenance:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesEquipmentMaintenance mesEquipmentMaintenance)
    {
        startPage();
        List<MesEquipmentMaintenance> list = mesEquipmentMaintenanceService.selectMesEquipmentMaintenanceList(mesEquipmentMaintenance);
        return getDataTable(list);
    }

    /**
     * 导出设备维护记录列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:maintenance:export')")
    @Log(title = "设备维护记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesEquipmentMaintenance mesEquipmentMaintenance)
    {
        List<MesEquipmentMaintenance> list = mesEquipmentMaintenanceService.selectMesEquipmentMaintenanceList(mesEquipmentMaintenance);
        ExcelUtil<MesEquipmentMaintenance> util = new ExcelUtil<MesEquipmentMaintenance>(MesEquipmentMaintenance.class);
        util.exportExcel(response, list, "设备维护记录数据");
    }

    /**
     * 获取设备维护记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:maintenance:query')")
    @GetMapping(value = "/{maintenanceId}")
    public AjaxResult getInfo(@PathVariable("maintenanceId") Long maintenanceId)
    {
        return success(mesEquipmentMaintenanceService.selectMesEquipmentMaintenanceByMaintenanceId(maintenanceId));
    }

    /**
     * 新增设备维护记录
     */
    @PreAuthorize("@ss.hasPermi('equipment:maintenance:add')")
    @Log(title = "设备维护记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesEquipmentMaintenance mesEquipmentMaintenance)
    {
        return toAjax(mesEquipmentMaintenanceService.insertMesEquipmentMaintenance(mesEquipmentMaintenance));
    }

    /**
     * 修改设备维护记录
     */
    @PreAuthorize("@ss.hasPermi('equipment:maintenance:edit')")
    @Log(title = "设备维护记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesEquipmentMaintenance mesEquipmentMaintenance)
    {
        return toAjax(mesEquipmentMaintenanceService.updateMesEquipmentMaintenance(mesEquipmentMaintenance));
    }

    /**
     * 删除设备维护记录
     */
    @PreAuthorize("@ss.hasPermi('equipment:maintenance:remove')")
    @Log(title = "设备维护记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{maintenanceIds}")
    public AjaxResult remove(@PathVariable Long[] maintenanceIds)
    {
        return toAjax(mesEquipmentMaintenanceService.deleteMesEquipmentMaintenanceByMaintenanceIds(maintenanceIds));
    }

    /**
     * 校验维护编号
     */
    @PostMapping("/checkMaintenanceCodeUnique")
    public AjaxResult checkMaintenanceCodeUnique(@RequestBody MesEquipmentMaintenance mesEquipmentMaintenance)
    {
        String result = mesEquipmentMaintenanceService.checkMaintenanceCodeUnique(mesEquipmentMaintenance.getMaintenanceCode());
        if ("1".equals(result))
        {
            return error("维护编号已存在");
        }
        return success();
    }

    /**
     * 根据设备ID查询维护记录列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:maintenance:list')")
    @GetMapping("/listByEquipmentId/{equipmentId}")
    public AjaxResult listByEquipmentId(@PathVariable Long equipmentId)
    {
        List<MesEquipmentMaintenance> list = mesEquipmentMaintenanceService.selectMaintenanceByEquipmentId(equipmentId);
        return success(list);
    }

    /**
     * 根据维护编号查询维护记录
     */
    @PreAuthorize("@ss.hasPermi('equipment:maintenance:query')")
    @GetMapping("/code/{maintenanceCode}")
    public AjaxResult getByCode(@PathVariable String maintenanceCode)
    {
        MesEquipmentMaintenance maintenance = mesEquipmentMaintenanceService.selectMaintenanceByCode(maintenanceCode);
        return success(maintenance);
    }

    /**
     * 根据维护类型查询维护记录列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:maintenance:list')")
    @GetMapping("/listByType/{maintenanceType}")
    public AjaxResult listByType(@PathVariable String maintenanceType)
    {
        MesEquipmentMaintenance mesEquipmentMaintenance = new MesEquipmentMaintenance();
        mesEquipmentMaintenance.setMaintenanceType(maintenanceType);
        List<MesEquipmentMaintenance> list = mesEquipmentMaintenanceService.selectMesEquipmentMaintenanceList(mesEquipmentMaintenance);
        return success(list);
    }

    /**
     * 根据维护状态查询维护记录列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:maintenance:list')")
    @GetMapping("/listByStatus/{status}")
    public AjaxResult listByStatus(@PathVariable String status)
    {
        MesEquipmentMaintenance mesEquipmentMaintenance = new MesEquipmentMaintenance();
        mesEquipmentMaintenance.setStatus(status);
        List<MesEquipmentMaintenance> list = mesEquipmentMaintenanceService.selectMesEquipmentMaintenanceList(mesEquipmentMaintenance);
        return success(list);
    }
}